package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.Pais;

public class PaisDaoImplementation implements PaisDao {

	public void save(Pais pais) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(pais);
		transaction.commit();
		session.close();
	}

	public void update(Pais pais) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(pais);
		transaction.commit();
		session.close();
	}

	public void delete(Pais pais) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(pais);
		transaction.commit();
		session.close();
	}

	public void consult(Pais pais) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		pais = (Pais) session.load(Pais.class, new String("Pais"));
	}

	public Pais getPais(long idPais) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Pais) session.load(Pais.class, idPais);
	}
	
	public List<Pais> list() 
	{
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();
	String sql = "SELECT new Pais( p.idPais, p.descricao, p.ddi, p.sigla ) FROM Pais p";
	List<Pais> lista = session.createQuery(sql).list();
	transaction.commit();
	session.close();
	return lista;
	}
	
	

}
